home *** CD-ROM | disk | FTP | other *** search
- /*
- * Present a dialog box with a text rectangle, a scroll bar, and an OK
- * button. This can be used as a help dialog. Pass in a dialog
- * resource number and a handle to the text. The dialog should
- * have the OK button as the first item, and user items for the
- * second and third items. The second is the rect in which the
- * control is drawn, the third is the rect in which text is drawn.
- * Only the OK button should be enabled.
- *
- * The text handle should be locked before calling TextDlog and
- * unlocked after.
- */
-
- # include "TransSkel.h"
-
- # include "BlobMgr.h"
- # include "BlobDemo.h"
-
-
- enum /* item numbers */
- {
- iOK = 1, /* OK button */
- iScrollBar, /* scroll bar user item */
- iTextRect, /* text rectangle user item */
- iOutline /* OK button user item */
- };
-
-
- static ControlHandle theScroll;
- static Boolean needScroll; /* true if need scroll bar */
- static short curLine;
- static short halfPage;
- static TEHandle teHand;
-
-
- /*
- * Proc for drawing scroll bar user item.
- */
-
- static pascal void
- DrawScroll (DialogPtr theDialog, short itemNo)
- {
- if (needScroll)
- ShowControl (theScroll);
- }
-
-
- /*
- * Proc for drawing text display user item.
- */
-
- static pascal void
- DrawTextRect (DialogPtr theDialog, short itemNo)
- {
- Rect r;
-
- SkelGetDlogRect (theDialog, iTextRect, &r);
- if (needScroll)
- FrameRect (&r);
- r = (**teHand).viewRect;
- TEUpdate (&r, teHand);
- }
-
-
- /*
- * Scroll to the correct position. lDelta is the
- * amount to CHANGE the current scroll setting by.
- */
-
- static void
- DoScroll (short lDelta)
- {
- short newLine;
-
- newLine = curLine + lDelta;
- if (newLine < 0) newLine = 0;
- if (newLine > GetCtlMax (theScroll)) newLine = GetCtlMax (theScroll);
- SetCtlValue (theScroll, newLine);
- lDelta = (curLine - newLine ) * (**teHand).lineHeight;
- TEScroll (0, lDelta, teHand);
- curLine = newLine;
- }
-
-
- static pascal void
- __TrackScroll (ControlHandle theScroll, short partCode)
- {
- short lDelta;
-
- if (partCode == GetCRefCon (theScroll)) /* still in same part? */
- {
- switch (partCode)
- {
- case inUpButton: lDelta = -1; break;
- case inDownButton: lDelta = 1; break;
- case inPageUp: lDelta = -halfPage; break;
- case inPageDown: lDelta = halfPage; break;
- }
- DoScroll (lDelta);
- }
- }
-
-
- /*
- * Filter to handle hits in scroll bar
- */
-
- static pascal Boolean
- TextFilter (DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
- {
- Point thePoint;
- short thePart;
-
- if (theEvent->what == mouseDown)
- {
- thePoint = theEvent->where;
- GlobalToLocal (&thePoint);
- thePart = TestControl (theScroll, thePoint);
- if (thePart == inThumb)
- {
- (void) TrackControl (theScroll, thePoint, nil);
- DoScroll (GetCtlValue (theScroll) - curLine);
- }
- else if (thePart != 0)
- {
- SetCRefCon (theScroll, (long) thePart);
- (void) TrackControl (theScroll, thePoint, __TrackScroll);
- }
- }
- return (false);
- }
-
-
- void
- TextDialog (short dlogNum, Handle textHandle,
- short fontNum, short fontSize, Boolean wrap)
- {
- GrafPtr oldPort;
- DialogPtr theDialog;
- short itemNo;
- Rect r;
- short scrollLines;
- short viewLines;
- Handle oldHText;
- ModalFilterProcPtr filterProc = (ModalFilterProcPtr) nil;
-
- GetPort (&oldPort);
- theDialog = GetNewDialog (dlogNum, nil, (WindowPtr) -1L);
- if (theDialog == (DialogPtr) nil)
- {
- SysBeep (1);
- return;
- }
-
- SkelPositionWindow (theDialog, skelPositionOnParentDevice,
- FixRatio (1, 2), FixRatio (1, 5));
-
- /* install OK button outliner */
- SkelSetDlogButtonOutliner (theDialog, iOutline);
-
- SkelSetDlogProc (theDialog, iTextRect, DrawTextRect);
-
- /*
- * Incorporate text into a TERec.
- */
- SetPort (theDialog);
- TextFont (fontNum);
- TextSize (fontSize);
- SkelGetDlogRect (theDialog, iTextRect, &r);
- InsetRect (&r, 4, 4);
- teHand = TENew (&r, &r);
- if (!wrap)
- (**teHand).crOnly = -1;
- oldHText = (**teHand).hText; /* save this. restore later */
- (**teHand).hText = textHandle;
- TECalText (teHand);
- viewLines = (r.bottom - r.top) / (**teHand).lineHeight;
- scrollLines = (**teHand).nLines - viewLines;
- needScroll = (scrollLines > 0);
- SkelSetDlogProc (theDialog, iScrollBar, DrawScroll);
- SkelGetDlogRect (theDialog, iScrollBar, &r);
- if (needScroll)
- {
- theScroll = NewControl (theDialog, &r, "\p", false, 0, 0, 0,
- scrollBarProc, 0L);
- SetCtlMax (theScroll, scrollLines);
- halfPage = viewLines / 2;
- curLine = 0;
- filterProc = TextFilter;
- }
-
- ShowWindow (theDialog);
- ModalDialog (SkelDlogFilter (filterProc, true), &itemNo);
- SkelRmveDlogFilter ();
-
- /*
- * Restore hText field of TE record before disposing of it.
- */
- (**teHand).hText = oldHText;
- TEDispose (teHand);
- /*ReleaseResource (textHandle);*/
- if (needScroll)
- DisposeControl (theScroll);
- SetPort (oldPort);
- DisposDialog(theDialog);
- }
-